package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xgkj.diyiketang.data.HuoDongInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.R;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/5/26.
 */
public class YuefanListAdapter extends BaseAdapter{
    private Context mContext;
    private final String TAG = getClass().getSimpleName();
    private List<HuoDongInfo> mList = new ArrayList<HuoDongInfo>();

    public YuefanListAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<HuoDongInfo> list){
        if (mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null && mList.size()>0){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size()>0){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null || convertView.getTag() == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.yuefanlist_item,null);
            viewHolder.headImage = (ImageView) convertView.findViewById(R.id.head_img);
            viewHolder.nickText = (TextView) convertView.findViewById(R.id.faqiren_name);
            viewHolder.sellerLogo = (ImageView) convertView.findViewById(R.id.seller_logo);
            viewHolder.titleText = (TextView) convertView.findViewById(R.id.huodong_name);
            viewHolder.addressText = (TextView) convertView.findViewById(R.id.huodong_addr);
            viewHolder.priceText = (TextView) convertView.findViewById(R.id.price);
            viewHolder.timeText = (TextView) convertView.findViewById(R.id.huodong_time);
            viewHolder.mProgress = (ProgressBar) convertView.findViewById(R.id.progress);
            viewHolder.baomingText = (TextView) convertView.findViewById(R.id.yibaoming);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        HuoDongInfo info = mList.get(position);
        if (info != null){
            String headLogoUrl = info.getUserLogo();
            if (headLogoUrl != null && headLogoUrl.length() != 0) {
                ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 40), ViewUtil.dip2px(mContext, 40));
                Bitmap cache_head = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(headLogoUrl,size));
                if (cache_head != null){
                    viewHolder.headImage.setImageBitmap(cache_head);
                }else {
                    viewHolder.headImage.setImageResource(R.drawable.default_head_image);
                }
                ImageCache.getInstance(mContext).loadBitmapToView(viewHolder.headImage, headLogoUrl, size);
            }

            try {
                viewHolder.nickText.setText(URLDecoder.decode(info.getNickName(),"utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            String seller_logo = info.getBusinessLogo();
            if (seller_logo != null && seller_logo.length() != 0){
                ViewUtil.Size seller_size = ViewUtil.getImageSize(mContext,R.drawable.default_big_image);
                Bitmap cache_seller_logo = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(seller_logo,seller_size));
                if (cache_seller_logo != null){
                    viewHolder.sellerLogo.setImageBitmap(cache_seller_logo);
                }else {
                    viewHolder.sellerLogo.setImageResource(R.drawable.zhanweitu);
                }

                ImageCache.getInstance(mContext).loadBitmapToView(viewHolder.sellerLogo,seller_logo,seller_size);
            }

            ViewUtil.Size size = ViewUtil.getImageSize(mContext,R.drawable.default_big_image);
            PalLog.e(TAG,size.width+"*******"+size.height);

            viewHolder.titleText.setText(info.getActivityTitle());
            viewHolder.priceText.setText(info.getFee());
            viewHolder.addressText.setText(info.getBusinessName());
            viewHolder.timeText.setText(info.getStartTime());
            viewHolder.baomingText.setText("已有"+info.getSignCount()+"人报名");
            int c = 0;
            int max = 0;
            try {
                c = Integer.parseInt(info.getSignCount());
                max = Integer.parseInt(info.getMaxCount());
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

            int percent = 0;
            if (max != 0) {
                percent = c * 100 / max;
            }
            viewHolder.mProgress.setProgress(percent);
        }



        return convertView;
    }

    class ViewHolder{
        ImageView headImage;
        TextView nickText;
        Button attentionBtn;
        ImageView sellerLogo;
        TextView titleText;
        TextView addressText;
        TextView priceText;
        TextView timeText;
        ProgressBar mProgress;
        TextView baomingText;
    }
}
